package com.oa.gongwen.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.oa.gongwen.biz.OfficialDocumentBiz;
import com.oa.gongwen.entity.AuditPerson;
import com.oa.gongwen.entity.Gongwen;
import com.oa.login.entity.Emp;


@Controller
@RequestMapping("/gongwen")
public class OfficialDocumentController {
      
	@Resource
	private OfficialDocumentBiz oBiz;
	
	private final static int PAGE_SIZE =10;
	
	/*
	 * 保存公文
	 */
	@RequestMapping("/bc")
	public void save(int options,String url,String title,String content,
			HttpServletResponse response,HttpServletRequest request,
			HttpSession session){
		Gongwen gongwen=new Gongwen();
		//String author=(String) session.getAttribute("name");
		Emp emp=(Emp) session.getAttribute("emp");
		String author=emp.getEmpName();
		if(author!=null){
			//非空不做处理
		}else {
			author="测试者";
		}
		gongwen.setAuthor(author);
		gongwen.setTitle(title);
		gongwen.setContent(content);
		gongwen.setGaddress(url);
		gongwen.setGrade(options);
		gongwen.setState(1);
		oBiz.save(gongwen);
		try {
			response.getWriter().write("success");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 保存草稿
	 */
	@RequestMapping("/cg")
	public void saveS(int options,String url,String title,String content,
			HttpServletResponse response,
			HttpServletRequest request,HttpSession session){
		Gongwen gongwen=new Gongwen();
		//String author=(String) session.getAttribute("name");
		Emp emp=(Emp) session.getAttribute("emp");
		String author=emp.getEmpName();
		if(author!=null){
			//非空不做处理
		}else {
			author="测试者";
		}
		
		gongwen.setAuthor(author);
		gongwen.setTitle(title);
		gongwen.setContent(content);
		gongwen.setGaddress(url);
		gongwen.setGrade(options);
		gongwen.setState(2);
		oBiz.save(gongwen);
		try {
			response.getWriter().write("success");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 查看公文列表页面
	 */
	@RequestMapping("/lookgw")
	public String selectGongwen(@RequestParam(required = false)Integer pageno,HttpServletRequest request,HttpServletResponse response,
			HttpSession session,Model model){
		    if (pageno == null){
				pageno = 1;
			}
			model.addAttribute("pageno", pageno);
			pageno = (pageno-1) * PAGE_SIZE;
			int count=oBiz.count();
			int temp = count%PAGE_SIZE;
			int totalPage = 0;
			if (temp == 0){
				totalPage = count/PAGE_SIZE;
			} else {
				totalPage = count/PAGE_SIZE + 1;
			}
			model.addAttribute("count", totalPage);
			Integer state = 1;
			List<Gongwen> gongwen=oBiz.selectGW(state,pageno);
			model.addAttribute("gw", gongwen);
	        return "gongwen/lookgw";
	}
	
	/*
	 * 查看公文详情页面
	 */
	@RequestMapping("/justlook")
	public String justlook(Integer id,HttpServletRequest request,HttpServletResponse response,
			Model model){
		Gongwen justlook=oBiz.justlook(id);
		List<AuditPerson> auditPerson=oBiz.selectAuditPerson(id);
		model.addAttribute("audit", auditPerson);
		model.addAttribute("gws", justlook);
		return "gongwen/justlook";
	}
	
	/*
	 * 审批公文列表页面
	 */
	@RequestMapping("/looklist")
	public String selectGWList(HttpServletRequest request,HttpServletResponse response,
			Model model,HttpSession session){
		    List<Gongwen> doList=oBiz.selectGW(1,2);
		    model.addAttribute("dolist", doList);
	        return "gongwen/looklist";
	}
	/*
	 * 审批公文实现页面
	 */
	@RequestMapping("/looklists")
	public String doGWg(int id,HttpServletRequest request,HttpServletResponse response,
			Model model,HttpSession session){
		   Gongwen shenhe= oBiz.justlook(id);
		   Integer ids=shenhe.getGwId();
		   String name=(String) session.getAttribute("uname");
		   String names=shenhe.getAuthor();
		    if (name.equals(names)) {
			return "gongwen/cuowu";
		   }
		    List<AuditPerson> auditPerson=oBiz.selectAuditPerson(ids);
		    model.addAttribute("person",auditPerson);
		    model.addAttribute("shenhe",shenhe);
	        return "gongwen/looklists";
	}
	
	/*
	 * 审批公文与审核是否通过
	 */
	@RequestMapping("/instructions")
	public void instructions(Integer id,String content,Integer options,HttpServletRequest request,HttpServletResponse response,
			Model model,HttpSession session){
		    String name=(String) session.getAttribute("uname");
		    
		    Gongwen gongwen=new Gongwen();
		    AuditPerson auditPerson=new AuditPerson();
		    auditPerson.setGongwenId(id);
		    auditPerson.setApAgree(options);
		    auditPerson.setOpinion(content);
		    auditPerson.setPerson(name);
		    gongwen.setGwId(id);
		    gongwen.setAuthor(name);
		    gongwen.setAgree(options);
		    oBiz.updategongwen(gongwen);
		    oBiz.insert(auditPerson);
		    try{
		    response.getWriter().write("success");
		    }catch (Exception e) {
				try {
					response.getWriter().write("fail");
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		   
		    
	}
	
	/*
	 * 查看退回公文列表页面
	 */
	@RequestMapping("/breakgw")
	public String selectGWLists(HttpServletRequest request,HttpServletResponse response,
			Model model,HttpSession session){
		    List<Gongwen> doList=oBiz.selectBydo(1,0);
		    model.addAttribute("dolists", doList);
	        return "gongwen/breakgw";
	}
	
	/*
	 * 退回公文修改页面
	 */
	@RequestMapping("/change")
	public String changeGW(Integer id,HttpServletRequest request,HttpServletResponse response,
			HttpSession session){
		    Gongwen gongwens=oBiz.justlook(id);
		    request.setAttribute("changegw", gongwens);
	        return "gongwen/changegw";
	}
	
	/*
	 * 退回功能之修改后提交操作处理
	 */
	@RequestMapping("/xiugai")
	public void xiugai(int id,String title,String content,HttpServletResponse response,
			HttpSession session){
		Gongwen gongwen=new Gongwen();
		gongwen.setGwId(id);
		gongwen.setContent(content);
		gongwen.setTitle(title);
		gongwen.setState(1);
		oBiz.updategongwen(gongwen);
		try {
			response.getWriter().write("success");
		} catch (IOException e) {
		
			e.printStackTrace();
		}
	}
	
	/*
	 * 查看草稿箱列表
	 */
	@RequestMapping("/lookcg")
	public String caogaoxiang(@RequestParam(required = false)Integer pageno,HttpServletRequest request,HttpServletResponse response,
			HttpSession session,Model model){
		if (pageno == null){
			pageno = 1;
		}
		model.addAttribute("pageno", pageno);
		pageno = (pageno-1) * PAGE_SIZE;
		int count=oBiz.count2();
		int temp = count%PAGE_SIZE;
		int totalPage = 0;
		if (temp == 0){
			totalPage = count/PAGE_SIZE;
		} else {
			totalPage = count/PAGE_SIZE + 1;
		}
		model.addAttribute("count", totalPage);
		
		    Emp name=(Emp) session.getAttribute("emp");
		    String names=name.getEmpName();
		    System.out.println(names);
		    List<Gongwen> gongwen=oBiz.selectGWs(2,names,pageno);
		    model.addAttribute("cg", gongwen);
	        return "gongwen/lookcg";
	}
	/*
	 *删除草稿
	 */
	@RequestMapping("/dogw")
	public String updateGW(Integer id,HttpServletRequest request,HttpServletResponse response,
			HttpSession session){
		   Gongwen gongwen1=oBiz.justlook(id);
		    request.setAttribute("dogw", gongwen1);
	        return "gongwen/xiugaicg";
	}
	
	/*
	 * 删除
	 */
	@RequestMapping("/delete.do")
	public void delete(Integer id,HttpServletRequest request,HttpServletResponse response,
			HttpSession session){
		     oBiz.delete(id);	
		     System.out.println(id);
	     try {
			response.getWriter().write("success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
